// 游戏设置对象
const GameSettings = {
    // 音频设置
    audio: {
        bgmVolume: 50,
        sfxVolume: 50,
        bgmEnabled: true,
        sfxEnabled: true
    },
    // 界面设置
    ui: {
        cardSize: 'medium',
        showTimer: true,
        showCardCount: true
    },
    // 游戏设置
    game: {
        autoPass: false,
        autoReady: false
    }
};

// 加载设置
function loadSettings() {
    const savedSettings = localStorage.getItem('gameSettings');
    if (savedSettings) {
        Object.assign(GameSettings, JSON.parse(savedSettings));
    }
    
    // 更新UI
    $('#bgmVolume').val(GameSettings.audio.bgmVolume);
    $('#sfxVolume').val(GameSettings.audio.sfxVolume);
    $('#bgmEnabled').prop('checked', GameSettings.audio.bgmEnabled);
    $('#sfxEnabled').prop('checked', GameSettings.audio.sfxEnabled);
    
    $('#cardSize').val(GameSettings.ui.cardSize);
    $('#showTimer').prop('checked', GameSettings.ui.showTimer);
    $('#showCardCount').prop('checked', GameSettings.ui.showCardCount);
    
    $('#autoPass').prop('checked', GameSettings.game.autoPass);
    $('#autoReady').prop('checked', GameSettings.game.autoReady);
    
    // 应用音频设置
    updateAudioSettings();
}

// 保存设置
function saveSettings() {
    // 获取音频设置
    GameSettings.audio.bgmVolume = parseInt($('#bgmVolume').val());
    GameSettings.audio.sfxVolume = parseInt($('#sfxVolume').val());
    GameSettings.audio.bgmEnabled = $('#bgmEnabled').prop('checked');
    GameSettings.audio.sfxEnabled = $('#sfxEnabled').prop('checked');
    
    // 获取界面设置
    GameSettings.ui.cardSize = $('#cardSize').val();
    GameSettings.ui.showTimer = $('#showTimer').prop('checked');
    GameSettings.ui.showCardCount = $('#showCardCount').prop('checked');
    
    // 获取游戏设置
    GameSettings.game.autoPass = $('#autoPass').prop('checked');
    GameSettings.game.autoReady = $('#autoReady').prop('checked');
    
    // 保存到本地存储
    localStorage.setItem('gameSettings', JSON.stringify(GameSettings));
    
    // 应用设置
    updateAudioSettings();
    updateUISettings();
    
    // 显示保存成功提示
    showToast('设置已保存');
}

// 更新音频设置
function updateAudioSettings() {
    if (window.audioManager) {
        audioManager.updateVolume('bgm', GameSettings.audio.bgmVolume / 100);
        audioManager.updateVolume('sfx', GameSettings.audio.sfxVolume / 100);
        
        if (!GameSettings.audio.bgmEnabled) {
            audioManager.stop('background');
        } else if (audioManager.sounds.background.paused) {
            audioManager.play('background');
        }
    }
}

// 更新界面设置
function updateUISettings() {
    document.documentElement.style.setProperty('--card-size', getCardSize());
    $('.timer').toggle(GameSettings.ui.showTimer);
    $('.card-count').toggle(GameSettings.ui.showCardCount);
}

// 获取卡牌大小
function getCardSize() {
    switch(GameSettings.ui.cardSize) {
        case 'small': return '40px';
        case 'large': return '80px';
        default: return '60px';
    }
}

// 显示提示消息
function showToast(message) {
    const toast = $('<div class="toast">')
        .text(message)
        .appendTo('body');
    
    setTimeout(() => toast.remove(), 2000);
}

// 页面加载完成后初始化
$(document).ready(function() {
    loadSettings();
    
    // 添加音量滑块实时预览
    $('#bgmVolume, #sfxVolume').on('input', function() {
        const type = this.id === 'bgmVolume' ? 'bgm' : 'sfx';
        if (window.audioManager) {
            audioManager.updateVolume(type, this.value / 100);
        }
    });
}); 